//package com.th.system.controller;

package com.th.admin.controller.auth;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.th.system.domain.UsUser;
import com.th.system.service.IUsUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.th.common.annotation.Log;
import com.th.common.core.controller.BaseController;
import com.th.common.core.domain.AjaxResult;
import com.th.common.enums.BusinessType;
import com.th.auth.domain.UsUserAuthApply;
import com.th.auth.service.IUsUserAuthApplyService;
//import com.th.system.domain.UsUserAuthApply;
//import com.th.system.service.IUsUserAuthApplyService;
import com.th.common.utils.poi.ExcelUtil;
import com.th.common.core.page.TableDataInfo;

/**
 * 客户认证申请Controller
 *
 * @author ruoyi
 * @date 2023-06-24
 */
@RestController
@RequestMapping("/system/apply")
public class UsUserAuthApplyController extends BaseController
{
    @Autowired
    private IUsUserAuthApplyService usUserAuthApplyService;

    @Autowired
    private IUsUserService usUserService;

    /**
     * 查询客户认证申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(UsUserAuthApply usUserAuthApply)
    {
        startPage();
        List<UsUserAuthApply> list = usUserAuthApplyService.selectUsUserAuthApplyList(usUserAuthApply);
        return getDataTable(list);
    }

    /**
     * 导出客户认证申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:export')")
    @Log(title = "客户认证申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UsUserAuthApply usUserAuthApply)
    {
        List<UsUserAuthApply> list = usUserAuthApplyService.selectUsUserAuthApplyList(usUserAuthApply);
        ExcelUtil<UsUserAuthApply> util = new ExcelUtil<UsUserAuthApply>(UsUserAuthApply.class);
        util.exportExcel(response, list, "客户认证申请数据");
    }

    /**
     * 获取客户认证申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(usUserAuthApplyService.selectUsUserAuthApplyById(id));
    }

    /**
     * 新增客户认证申请
     */
    @PreAuthorize("@ss.hasPermi('system:apply:add')")
    @Log(title = "客户认证申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UsUserAuthApply usUserAuthApply)
    {
        return toAjax(usUserAuthApplyService.insertUsUserAuthApply(usUserAuthApply));
    }

    /**
     * 修改客户认证申请
     */
    @PreAuthorize("@ss.hasPermi('system:apply:edit')")
    @Log(title = "客户认证申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UsUserAuthApply usUserAuthApply)
    {
        // 获取当前时间
        Date now = new Date();
        usUserAuthApply.setExamineTime(now);
        // 顺便把 us_user表的角色也改了
        Long userId=usUserAuthApply.getUserId();
        UsUser usUser=usUserService.selectUsUserById(userId);
        if(usUserAuthApply.getExamineResultStatus()==2){
            // 如果审核通过，则根据申请修改角色
            Long authType=usUserAuthApply.getAuthType();
            usUser.setUserRole(authType+1);
            usUserService.updateUsUser(usUser);
        }else if(usUserAuthApply.getExamineResultStatus()==3){
            // 审核不通过，变为游客
            usUser.setUserRole(1L);
            usUserService.updateUsUser(usUser);
        }
        return toAjax(usUserAuthApplyService.updateUsUserAuthApply(usUserAuthApply));
    }

    /**
     * 删除客户认证申请
     */
    @PreAuthorize("@ss.hasPermi('system:apply:remove')")
    @Log(title = "客户认证申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(usUserAuthApplyService.deleteUsUserAuthApplyByIds(ids));
    }
}
